/*****************************************************
This program was produced by the
CodeWizardAVR V2.04.4a Advanced
Automatic Program Generator
 Copyright 1998-2009 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 2011/08/10
Author  : AliReza
Company : Mahdavi
Comments: 


Chip type               : ATmega32
Program type            : Application
AVR Core Clock frequency: 12.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 512
*****************************************************/

#include <mega32.h>
#include <math.h>
#include <delay.h>
#include <stdlib.h>
#include <stdio.h>


#define LCD_PORT PORTB
#define LCD_RST  PORTC.5
#define LCD_E    PORTC.4
#define LCD_RW   PORTC.3
#define LCD_RS   PORTC.2
#define LCD_CS2  PORTC.1
#define LCD_CS1  PORTC.0

void AVR_INIT();
void write_lcd (char columns , char page,char data);
void glcd_xy (int x , int y );
void glcd_rQ (int r , float Q );
void latchlcd();
void glcd_init();
void glcd_clear();

unsigned char read_buffer[128][8];

void main(void)
{
int i=0;
int j=0;
AVR_INIT();

glcd_init();

glcd_clear();

glcd_xy(0,0);

//   30 
for (i=0;i<360;i++)
glcd_rQ(30,i);

delay_ms(1000);

//     20
for(i=-20;i<21;i++){
glcd_xy(-20,i);
glcd_xy(20,i); 
glcd_xy(i,20);
glcd_xy(i,-20);
} 

delay_ms(1000);

//   20   
for ( i=-20;i<21;i++){
for( j =-20;j<21;j++)  
    glcd_xy(i,j);
}


while (1);
}

void AVR_INIT(){

PORTA=0x00;
DDRA=0x00; 
PORTB=0x00;
DDRB=0xFF;
PORTC=0x00;
DDRC=0xFF;
PORTD=0x00;
DDRD=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
MCUCR=0x00;
MCUCSR=0x00;
TIMSK=0x00;
ACSR=0x80;
SFIOR=0x00;

}

void glcd_xy(int x , int y ){
int y2;

x = x + 64;
y = y + 32;


y2 = y%8 ;
y2 = pow(2,y2);

y = y/8 ;

y2 =   y2 | read_buffer[x][y] ;

write_lcd(x,y,y2);

}

void glcd_rQ (int r , float Q ){
glcd_xy(r*cos(Q), r*sin(Q));

}


void write_lcd(char columns , char page,char data){

read_buffer[columns][page] = data;


if(columns >= 64){
LCD_CS1=0;
LCD_CS2=1;
}else{
LCD_CS1=1;
LCD_CS2=0;
}
LCD_RS=0;
LCD_RW=0;
LCD_PORT=0xB8 | page;
latchlcd();

LCD_RS=0;
LCD_RW=0;
LCD_PORT=0x40 | columns;
latchlcd();

LCD_RS=1;
LCD_RW=0;
LCD_PORT=data;
latchlcd();
}

void latchlcd(){
delay_us(7);
LCD_E=1;
delay_us(7);
LCD_E=0;
}

void glcd_init(){
unsigned char i;
LCD_RST=0;
delay_ms(10);
LCD_RST=1;
delay_ms(100);
LCD_RS=0;
LCD_CS1=0;
LCD_CS2=1;
for(i=0;i<2;i++){
LCD_PORT=0x3E; //display off
latchlcd();
LCD_PORT=0x40; //colum address(y)=0
latchlcd();
LCD_PORT=0xB8; //page address(x)=0
latchlcd();
LCD_PORT=0xC0; //display startlin =0
latchlcd();
LCD_PORT=0x3F; //display on
latchlcd();
LCD_CS1=~LCD_CS1;
LCD_CS2=~LCD_CS2;
}
}

void glcd_clear(){
int j,x,y;
x=0;
y=0;
for( j=0;j<1024;j++){
        if(x>127){
                x=0;
                y++;
        }
        write_lcd(x,y,0);
        read_buffer[x][y]=0;
        x++;
}
} 
